package me.xplabs.model 
{
	import me.xplabs.Globals;
	import me.xplabs.model.combatunit.BaseFlag;
	import me.xplabs.model.combatunit.MFlagInfo;
	import me.xplabs.model.skill.BaseSkill;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.utils.SimpleObjPool;
	/**
	 * ...
	 * @author xiaohan
	 */
	public class FlagDataManager 
	{
		private var _flagPool:SimpleObjPool;
		private var _flags:Vector.<MFlagInfo>;
		public function FlagDataManager() 
		{
			_flags = new Vector.<MFlagInfo>();
			_flagPool = new SimpleObjPool(MFlagInfo, 1, 1);
		}
		
		public function update():void
		{
			var len:int = _flags.length;
			for (var i:int = len - 1; i >= 0 ; i--)
			{
				_flags[i].heartbeat();
			}
		}
		
		public function addFlag(p_baseFlag:BaseFlag, p_x:int, p_y:int):void
		{
			var tempFlag:MFlagInfo = _flagPool.getObj();
			flagAssignment(p_baseFlag, tempFlag);
			tempFlag.curPosX 		= p_x;
			tempFlag.curPosY 		= p_y;
			tempFlag.cellX			= p_x / Globals.cellW;
			tempFlag.cellY			= p_y / Globals.cellH;
			tempFlag.soleId			= Globals.soleId;
			var skills:Vector.<BaseSkill> = new Vector.<BaseSkill>();
			for (var j:int = 0; j < p_baseFlag.skillIds.length; j++) 
			{
				skills.push(GameDataSingleton.i.getBaseSkillById(p_baseFlag.skillIds[j]));
			}
			tempFlag.initSkillList(skills);
			_flags.push(tempFlag);
			sendMsg(NotificationName.FIGHT_NOTIFICATION, tempFlag, NotificationFight.ADD_FLAG_COMPLETE);
		}
		
		public function updateFlag(p_baseFlag:BaseFlag):void
		{
			
		}
		
		public function delFlag(p_soleId:int):void
		{
			var len:int = _flags.length;
			for (var i:int = 0; i < len; i++) 
			{
				if (_flags[i].soleId == p_soleId)
				{
					//移除旗帜
					removeFlag(_flags.splice(i, 1)[0]);
					break;
				}
			}
		}
		public function removeFlag(p_flag:MFlagInfo):void
		{
			sendMsg(NotificationName.FIGHT_NOTIFICATION, p_flag.soleId, NotificationFight.REMOVE_FLAG_COMPLETE);
			//RolePositionManager.i.deleteCellActor(p_actor.prevCellPos,p_actor.curCellPos, p_actor.soleId);
			Globals.delSoleId(p_flag.soleId);
			p_flag.dispose();
			_flagPool.dispose(p_flag);
			p_flag = null;
			//Utils.print("敌人消失啦");
		}
		
		public function dispose():void 
		{
			var len:int = _flags.length;
			for (var i:int = 0; i < len; i++) 
			{
				removeFlag(_flags[i]);
			}
			_flags.length = 0;
		}
		/**
		 * 旗帜属性赋值
		 * @param	flag1
		 * @param	flag2
		 */
		private function flagAssignment(flag1:BaseFlag, flag2:MFlagInfo):void
		{
			flag2.id             = flag1.id;        
			flag2.resId          = flag1.resId;     
			flag2.title          = flag1.title;     
			flag2.attackArea     = flag1.attackArea;
			flag2.describe       = flag1.describe;  
			flag2.skillIds       = flag1.skillIds;  
			flag2.flagType       = flag1.flagType;  
			flag2.flagIcon       = flag1.flagIcon;  
			flag2.areaW          = flag1.areaW;     
			flag2.areaH          = flag1.areaH;     
			flag2.areaCellW      = flag1.areaCellW; 
			flag2.areaCellH      = flag1.areaCellH; 
			flag2.flagLevel      = flag1.flagLevel; 
		}
	}

}